package leetcode_day._2021._202104._0110;

/**
 * @author yzh
 * @date 2021/4/8 8:56
 * 寻找旋转排序数组中的最小值
 * 使用二分查找
 * nums[mid] >= nums[high] , 往右边走 , low = mid + 1
 * nums[mid] < nums[high] , 往左走 , high = mid
 * nums[mid] < nums[mid - 1] && nums[mid] < nums[mid + 1] , 直接返回
 */
public class _08_153 {
    public static void main(String[] args) {
        int[] nums = new int[]{3, 1, 2};
        System.out.println(new _08_153().findMin(nums));
    }

    public int findMin(int[] nums) {
        int low = 0, high = nums.length - 1;
        while (low < high) {
            int mid = (low + high) >> 1;
//            if (mid - 1 >= 0 && mid + 1 <= nums.length - 1 && nums[mid] < nums[mid - 1] && nums[mid] < nums[mid + 1]) return nums[mid];
//            if (nums[mid] >= nums[high]) low = mid + 1;
//            else high = mid - 1;
            if (nums[mid] >= nums[high]) low = mid + 1;
            else high = mid;
        }
        return nums[low];
    }

}
